/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Xml;
using DaveSexton.DocProject.DocSites.Configuration;

namespace DaveSexton.DocProject.DocSites
{
	public static class DocSiteManager
	{
		#region Public Properties
		public static DocSiteSettings Settings
		{
			[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
			get
			{
				EnsureConfiguration();

				return settings;
			}
		}
		#endregion

		#region Private / Protected
		private static DocSiteSettings settings;
		#endregion

		#region Methods
		[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
		internal static void EnsureConfiguration()
		{
			if (settings == null)
			{
				DocSiteConfigurationSection section = (DocSiteConfigurationSection) WebConfigurationManager.GetSection("docSite");

				DocSiteSearchProviderCollection searchProviders = new DocSiteSearchProviderCollection();
				ProvidersHelper.InstantiateProviders(section.SearchProviders, searchProviders, typeof(DocSiteSearchProvider));

				DocSiteSearchProvider defaultSearchProvider = searchProviders[section.DefaultSearchProvider];

				if (defaultSearchProvider == null)
					defaultSearchProvider = new DocSiteMemorySearchProvider();

				string config;
				if (HttpRuntime.AppDomainAppId == null)
					// this is useful for testing the DocSite functions in a console application
					config = "DocSite.config";
				else
					config = System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, "DocSite.config");

				settings = new DocSiteSettings(config);

				DocSiteSearch.searchProviders = searchProviders;
				DocSiteSearch.defaultSearchProvider = defaultSearchProvider;

				UpdateCachedSettings();
			}
		}

		[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
		public static void UpdateCachedSettings()
		{
			DocSiteSearch.UpdateCachedSettings();

			DocSiteNavigator.UpdateCachedSettings();
		}
		#endregion
	}
}
